package com.ttt.gdfood.mapper;

import com.ttt.gdfood.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();


    @Select("select * from user where username=#{username}")
    User findByUsername(String username);

    @Select("select * from user where id=#{id}")
    @Results({
            @Result(property = "contactNum", column = "id", one = @One(select = "com.ttt.gdfood.mapper.ContactMapper.getContactNum")),
            @Result(property = "fansNum", column = "id", one = @One(select = "com.ttt.gdfood.mapper.ContactMapper.getFansNum")),
            @Result(property = "thumbsNum", column = "id", one = @One(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getThumbsNum")),
            @Result(property = "id",column = "id"),
    })
    User findById(int id);

    @Select("select * from user where phone=#{phone}")
    User findByPhone(String phone);

//    @Insert("INSERT into `user` (`username`, `phone`, `password`, `role`, `avatar`, `sex`, `region`, `per_signature`, `create_time`) VALUES (#{username},#{phone},#{password},#{role},#{sex},#{region},#{per_signature},#{create_time})")
//    @Transactional
//    void insertUser(User user);
    @Insert("INSERT into `user` (`username`, `phone`, `password`, `avatar`, `role`,`create_time`) VALUES (#{username},#{phone},#{password},#{avatar},#{role},#{create_time})")
    @Transactional
    void insertUser(User user);

    @Delete("DELETE FROM user WHERE id=#{id}")
    void deleteById(int id);

    @Update("update user set avatar=#{avatar},username=#{username},sex=#{sex},per_signature=#{per_signature},region=#{region} where id=#{id}")
    void updateUser(User user);

    // 后台修改用户信息
    @Update("update user set phone=#{phone},avatar=#{avatar},username=#{username},sex=#{sex},per_signature=#{per_signature},password=#{password} where id=#{id}")
    void updateUserInfo(User user);
}
